const mongoose = require('./model/db');
const Service = require('./model/Service');
const { User } = require('./model/model');

// 示例服务数据
const sampleServices = [
    {
        name: 'SPA护理',
        category: 'SPA',
        price: 398,
        duration: 120,
        description: '专业SPA护理，包含全身按摩和精油护理',
        image: '/images/spa.jpg',
        status: true
    },
    {
        name: '面部护理',
        category: '面部护理',
        price: 198,
        duration: 90,
        description: '深层清洁面部，补水保湿护理',
        image: '/images/facial.jpg',
        status: true
    },
    {
        name: '身体按摩',
        category: '身体按摩',
        price: 298,
        duration: 100,
        description: '全身放松按摩，缓解疲劳',
        image: '/images/massage.jpg',
        status: true
    },
    {
        name: '足疗',
        category: '足疗',
        price: 158,
        duration: 80,
        description: '专业足部护理和按摩',
        image: '/images/foot.jpg',
        status: true
    },
    {
        name: '美甲',
        category: '美甲',
        price: 128,
        duration: 60,
        description: '专业美甲服务，包含手部护理',
        image: '/images/nail.jpg',
        status: true
    },
    {
        name: '美发',
        category: '美发',
        price: 258,
        duration: 120,
        description: '专业美发造型服务',
        image: '/images/hair.jpg',
        status: true
    }
];

// 技师技能配置
const technicianSkills = {
    '技师2号': ['面部护理', '身体按摩', 'SPA'],
    '技师3号': ['足疗', '身体按摩', '美甲'],
    '技师4号': ['美发', '美甲', '面部护理'],
    '技师5号': ['SPA', '足疗', '身体按摩']
};

// 初始化数据函数
async function initData() {
    try {
        console.log('开始初始化数据...');

        // 清空现有服务数据
        await Service.deleteMany({});
        console.log('已清空现有服务数据');

        // 插入服务数据
        const services = await Service.insertMany(sampleServices);
        console.log(`已插入 ${services.length} 个服务项目`);

        // 为现有技师用户添加技能信息
        console.log('正在为技师用户添加技能信息...');

        // 获取所有role为689dc4e6889e4412b3bdfed2的用户
        const technicians = await User.find({
            role: '689dc4e6889e4412b3bdfed2',
            status: true
        });

        console.log(`找到 ${technicians.length} 个技师用户`);

        // 显示技师信息
        console.log('\n技师用户列表:');
        technicians.forEach(tech => {
            const skills = technicianSkills[tech.username] || ['面部护理', '身体按摩', '足疗'];
            console.log(`- ${tech.username}: ${skills.join(', ')}`);
        });

        console.log('\n数据初始化完成！');

        // 显示插入的服务数据
        console.log('\n服务项目列表:');
        services.forEach(service => {
            console.log(`- ${service.name}: ¥${service.price} (${service.duration}分钟)`);
        });

    } catch (error) {
        console.error('数据初始化失败:', error);
    } finally {
        // 关闭数据库连接
        mongoose.connection.close();
        console.log('数据库连接已关闭');
    }
}

// 运行初始化
if (require.main === module) {
    initData();
}

module.exports = { initData }; 